home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / FHOOKS.C < prev    next >
C/C++ Source or Header  |  1990-06-20  |  7KB  |  230 lines

  1. /* -*-C-*-
  2.  
  3. $Header: fhooks.c,v 9.32 90/06/20 17:40:31 GMT cph Exp $
  4.  
  5. Copyright (c) 1988, 1989, 1990 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* This file contains hooks and handles for the new fluid bindings
  36.    scheme for multiprocessors. */
  37.  
  38. #include "scheme.h"
  39. #include "prims.h"
  40. #include "trap.h"
  41. #include "lookup.h"
  42. #include "locks.h"
  43.  
  44. DEFINE_PRIMITIVE ("SET-FLUID-BINDINGS!", Prim_set_fluid_bindings, 1)
  45. {
  46.   PRIMITIVE_HEADER (1);
  47.   CHECK_ARG (1, APPARENT_LIST_P);
  48.   {
  49.     SCHEME_OBJECT result = Fluid_Bindings;
  50.     Fluid_Bindings = (ARG_REF (1));
  51.     PRIMITIVE_RETURN (result);
  52.   }
  53. }
  54.  
  55. DEFINE_PRIMITIVE ("GET-FLUID-BINDINGS", Prim_get_fluid_bindings, 0)
  56. {
  57.   PRIMITIVE_HEADER (0);
  58.   PRIMITIVE_RETURN (Fluid_Bindings);
  59. }
  60.  
  61. DEFINE_PRIMITIVE ("WITH-SAVED-FLUID-BINDINGS", Prim_with_saved_fluid_bindings, 1, 1, 0)
  62. {
  63.   PRIMITIVE_HEADER (1);
  64.   {
  65.     SCHEME_OBJECT thunk = (ARG_REF (1));
  66.     PRIMITIVE_CANONICALIZE_CONTEXT ();
  67.     POP_PRIMITIVE_FRAME (1);
  68.   Will_Push (CONTINUATION_SIZE + STACK_ENV_EXTRA_SLOTS + 1);
  69.     /* Save previous fluid bindings for later restore */
  70.     Store_Expression (Fluid_Bindings);
  71.     Store_Return (RC_RESTORE_FLUIDS);
  72.     Save_Cont ();
  73.     /* Invoke the thunk. */
  74.     STACK_PUSH (thunk);
  75.     STACK_PUSH (STACK_FRAME_HEADER);
  76.   Pushed ();
  77.     PRIMITIVE_ABORT (PRIM_APPLY);
  78.     /*NOTREACHED*/
  79.   }
  80. }
  81.  
  82. #define lookup_slot(environment, variable)                \
  83.   (lookup_cell ((OBJECT_ADDRESS (variable)), (environment)))
  84.  
  85. DEFINE_PRIMITIVE ("ADD-FLUID-BINDING!", Prim_add_fluid_binding, 3, 3,
  86.   "(ADD-FLUID-BINDING! ENVIRONMENT SYMBOL/VARIABLE VALUE)\n\
  87. Dynamically bind SYMBOL/VARIABLE to VALUE in ENVIRONMENT.\n\
  88. If SYMBOL/VARIABLE has not been \"fluidized\", do so first.")
  89. {
  90.   extern SCHEME_OBJECT * lookup_cell ();
  91.   static SCHEME_OBJECT new_fluid_binding ();
  92.   PRIMITIVE_HEADER (3);
  93.   CHECK_ARG (1, ENVIRONMENT_P);
  94.   {
  95.     fast SCHEME_OBJECT environment = (ARG_REF (1));
  96.     fast SCHEME_OBJECT name = (ARG_REF (2));
  97.     fast SCHEME_OBJECT * cell;
  98.     switch (OBJECT_TYPE (name))
  99.       {
  100.     /* The next two cases are a temporary fix since compiler doesn't
  101.        do scode-quote the same way that the interpreter does.
  102.  
  103.        Ultimately we need to redesign deep fluid-let support anyway,
  104.        so this will go away.
  105.        */
  106.  
  107.       case TC_LIST:
  108.     cell = (lookup_slot (environment, (PAIR_CAR (name))));
  109.     break;
  110.  
  111.       case TC_SCODE_QUOTE:
  112.     cell =
  113.       (lookup_slot
  114.        (environment, (FAST_MEMORY_REF (name, SCODE_QUOTE_OBJECT))));
  115.     break;
  116.  
  117.       case TC_VARIABLE:
  118.     cell = (lookup_slot (environment, name));
  119.     break;
  120.  
  121.       case TC_INTERNED_SYMBOL:
  122.       case TC_UNINTERNED_SYMBOL:
  123.     cell = (deep_lookup (environment, name, fake_variable_object));
  124.     break;
  125.  
  126.       default:
  127.     error_wrong_type_arg (2);
  128.       }
  129.     PRIMITIVE_RETURN (new_fluid_binding (cell, (ARG_REF (3)), false));
  130.   }
  131. }
  132.  
  133. static SCHEME_OBJECT
  134. new_fluid_binding (cell, value, force)
  135.      SCHEME_OBJECT * cell;
  136.      SCHEME_OBJECT value;
  137.      Boolean force;
  138. {
  139.   fast SCHEME_OBJECT trap;
  140.   Lock_Handle set_serializer;
  141.   SCHEME_OBJECT new_trap_value;
  142.   long new_trap_kind = TRAP_FLUID;
  143.   long trap_kind;
  144.   SCHEME_OBJECT saved_extension = SHARP_F;
  145.   SCHEME_OBJECT saved_value;
  146.  
  147.   setup_lock (set_serializer, cell);
  148.  
  149.  new_fluid_binding_restart:
  150.   trap = (*cell);
  151.   new_trap_value = trap;
  152.   if (REFERENCE_TRAP_P (trap))
  153.     {
  154.       get_trap_kind (trap_kind, trap);
  155.       switch (trap_kind)
  156.     {
  157.     case TRAP_DANGEROUS:
  158.       MEMORY_SET
  159.         (trap,
  160.          TRAP_TAG,
  161.          (LONG_TO_UNSIGNED_FIXNUM (TRAP_FLUID | (trap_kind & 1))));
  162.       /* Fall through */
  163.     case TRAP_FLUID:
  164.     case TRAP_FLUID_DANGEROUS:
  165.       new_trap_kind = -1;
  166.       break;
  167.  
  168.     case TRAP_UNBOUND:
  169.     case TRAP_UNBOUND_DANGEROUS:
  170.       if (! force)
  171.         {
  172.           remove_lock (set_serializer);
  173.           signal_error_from_primitive (ERR_UNBOUND_VARIABLE);
  174.         }
  175.       /* Fall through */
  176.     case TRAP_UNASSIGNED:
  177.     case TRAP_UNASSIGNED_DANGEROUS:
  178.       new_trap_kind = (TRAP_FLUID | (trap_kind & 1));
  179.       new_trap_value = UNASSIGNED_OBJECT;
  180.       break;
  181.  
  182.     case TRAP_COMPILER_CACHED:
  183.     case TRAP_COMPILER_CACHED_DANGEROUS:
  184.       saved_extension = (FAST_MEMORY_REF ((*cell), TRAP_EXTRA));
  185.       cell = (MEMORY_LOC (saved_extension, TRAP_EXTENSION_CELL));
  186.       update_lock (set_serializer, cell);
  187.       saved_value = (*cell);
  188.       if (REFERENCE_TRAP_P (saved_value))
  189.         /* No need to recache uuo links, they must already be recached. */
  190.         saved_extension = SHARP_F;
  191.       goto new_fluid_binding_restart;
  192.  
  193.     default:
  194.       remove_lock (set_serializer);
  195.       signal_error_from_primitive (ERR_ILLEGAL_REFERENCE_TRAP);
  196.     }
  197.     }
  198.  
  199.   if (new_trap_kind != -1)
  200.     {
  201.       if (GC_allocate_test (2))
  202.     {
  203.       remove_lock (set_serializer);
  204.       Primitive_GC (2);
  205.     }
  206.       trap = (MAKE_POINTER_OBJECT (TC_REFERENCE_TRAP, Free));
  207.       (*Free++) = (LONG_TO_UNSIGNED_FIXNUM (new_trap_kind));
  208.       (*Free++) = new_trap_value;
  209.       (*cell) = trap;
  210.     }
  211.   if (saved_extension != SHARP_F)
  212.     {
  213.       extern long recache_uuo_links ();
  214.       long value = (recache_uuo_links (saved_extension, saved_value));
  215.       if (value != PRIM_DONE)
  216.     {
  217.       remove_lock (set_serializer);
  218.       if (value == PRIM_INTERRUPT)
  219.         signal_interrupt_from_primitive ();
  220.       else
  221.         signal_error_from_primitive (value);
  222.     }
  223.     }
  224.   remove_lock (set_serializer);
  225.  
  226.   /* Fluid_Bindings is per processor private. */
  227.   Fluid_Bindings = (cons ((cons (trap, value)), Fluid_Bindings));
  228.   return (SHARP_F);
  229. }
  230.